<?php 

Class photoModel extends AppModel
{
	
	
	public function insert_photo($info)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
	
	     if(is_file(ROOT_PATH."/".$info['original_pic']))
	     {
	     	$info['middle_pic']=$this->make_thumb('/'.$info['original_pic'],'200','200');
	     	$info['small_pic']=$this->make_thumb('/'.$info['original_pic'],'80','80');
	     }
	
			
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
	
	
				$value.="'$v',";
					
			}
		}
			
			
		$field.="created";
		$value.="'".$_SERVER['REQUEST_TIME']."'";
			
		$sql="INSERT INTO ".$this->table('album_photos')." (".$field.") VALUES(".$value.")";
	
	
		return $res=$this->query($sql);
	}
	public function get_album_id($id)
	{
		$sql="SELECT cat_id FROM ".$this->table('album_photos')." WHERE id=".$id;
		return $this->getOne($sql);
	}
	
	public function get_photo($id)
	{
		$sql="SELECT p.*,c.name as cat_name FROM ".$this->table('album_photos')." as p LEFT JOIN ".$this->table('category')." as c ON p.cat_id=c.id WHERE p.id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			$arr=hy_dig_filter($result['title'],3);
			$arr2=hy_dig_filter($result['abstract'],3);
			
			$result['cat_name']=hy_dig_filter($result['cat_name'],1);
			$result['title']=$arr[0];
			$result['en_title']=$arr[1];
			$result['abstract']=$arr2[0];
			$result['en_abstract']=$arr2[1];
			return $result;
		}
		else
		{
			return $result=array();
		}
	}

	
	public function photo_order($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('album_photos')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	
	function del_photo($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
				
			$sql="DELETE FROM ".$this->table('album_photos')." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
				
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
		}
		return $result;
	
	
	}

	public function photo_list($page=1,$sort)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where='';
	   if($sort['cat_id']>0)
		{
			$where.=' AND cat_id='.$sort['cat_id'];
		}
		elseif($sort['cat_id']==-1)//未分类
		{
			$where.=' AND c.name is null';
		}
		if($sort['keyword']!='')
		{
			$where.=" AND title like '%".$sort['keyword']."%'";
		}
	 
		$result=$this->show_page("SELECT p.id,p.title,p.ordering,p.created,IFNULL(c.name,'".Lang('unclassifie')."') as name FROM (".$this->table('album_photos')." as p) LEFT JOIN ".$this->table('category')." as c ON p.cat_id=c.id WHERE 1=1 ".$where." ORDER BY p.id DESC",$page);
		if(!empty($result))
		{
		foreach($result['result'] as  $k=>$v)
		{
			$result['result'][$k]['title']=hy_dig_filter($v['title'],1);
			$result['result'][$k]['name']=hy_dig_filter($v['name'],1);
				
		}
		}
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
	  
	
		return $listPage;
	}
	public function update_photo($info,$id)
	{
		if($id)
		{
            $sql="SELECT original_pic FROM ".$this->table('album_photos')." WHERE id=".$id;
            $old_pic=$this->getOne($sql);
            if($old_pic!=$info['original_pic '])
            {
            	if(is_file(ROOT_PATH."/".$info['original_pic']))
            	{
            		$info['middle_pic']=$this->make_thumb('/'.$info['original_pic'],'200','200');
            		$info['small_pic']=$this->make_thumb('/'.$info['original_pic'],'80','80');
            	}
            }
			foreach($info as $k=>$v)
			{
	
				$where.=" $k='$v',";
	
	
			}
	
			$where.="modified='".$_SERVER['REQUEST_TIME']."'";
	
			$sql="UPDATE ".$this->table('album_photos')." SET ".$where." WHERE id='".$id."'";
	
			return $res=$this->query($sql);
		}
	}
}